﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmRoomInitConsumer : BaseForm
    {
        public RoomTypeInfo RoomType = null;

        public FrmRoomInitConsumer()
        {
            InitializeComponent();
            InitDictItem();
        }

        private void InitDictItem()
        {
            this.txtItemName.Items.Clear();
            string goodsType = SystemConfig.Default.KFInitConsumerGoodsType;
            List<GoodsInfo> goodList = BLLFactory<Goods>.Instance.FindByGoodsType(goodsType);
            foreach (GoodsInfo info in goodList)
            {
                this.txtItemName.Items.Add(new CListItem(info.Name, info.ID.ToString()));
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmRoomInitConsumer_Load(object sender, EventArgs e)
        {
            this.winGridView1.OnEditSelected += new EventHandler(winGridView1_OnEditSelected);
            this.winGridView1.OnDeleteSelected += new EventHandler(winGridView1_OnDeleteSelected);
            this.winGridView1.OnAddNew += new EventHandler(winGridView1_OnAddNew);
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;
            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;

            this.txtItemName.Focus();
            txtQuantity.Text = "1";
            if (RoomType != null)
            {
                this.lblRoomType.Text = RoomType.Name;
                this.lblPersons.Text = RoomType.BedsNumber.ToString();
                BindData();
            }
        }

        void winGridView1_OnAddNew(object sender, EventArgs e)
        {
            FrmEditInitConsumer dlg = new FrmEditInitConsumer();
            dlg.RoomType = RoomType.Name;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                BindData();
            }
        }

        void winGridView1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (this.winGridView1.dataGridView1.SelectedRows.Count == 0)
                return;

            if (MessageUtil.ShowYesNoAndWarning("您确定要删除选定的记录么？") != DialogResult.Yes)
                return;

            DataGridViewRow row = this.winGridView1.dataGridView1.SelectedRows[0];
            if (row != null && row.Cells["ID"].Value != null)
            {
                string id = row.Cells["ID"].Value.ToString();
                BLLFactory<InitConsumer>.Instance.Delete(id);
                BindData();
            }
        }

        void winGridView1_OnEditSelected(object sender, EventArgs e)
        {
            if (this.winGridView1.dataGridView1.SelectedRows.Count == 0)
                return;

            DataGridViewRow row = this.winGridView1.dataGridView1.SelectedRows[0];
            if (row != null && row.Cells["ID"].Value != null)
            {
                FrmEditInitConsumer dlg = new FrmEditInitConsumer();
                dlg.RoomType = RoomType.Name;
                dlg.ID = row.Cells["ID"].Value.ToString();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    BindData();
                }
            }
        }

        private void BindData()
        {
            if (RoomType != null)
            {
                #region 添加别名解析
                this.winGridView1.AddColumnAlias("ID", "编号");
                this.winGridView1.AddColumnAlias("RoomType", "房间类型");
                this.winGridView1.AddColumnAlias("ItemName", "项目名称");
                this.winGridView1.AddColumnAlias("Price", "单价");
                this.winGridView1.AddColumnAlias("Discount", "打折比例");
                this.winGridView1.AddColumnAlias("DiscountPrice", "折后单价");
                this.winGridView1.AddColumnAlias("Quantity", "数量");
                this.winGridView1.AddColumnAlias("Amount", "金额");
                this.winGridView1.AddColumnAlias("Note", "备注信息");
                #endregion
                this.winGridView1.DisplayColumns = "RoomType,ItemName,Price,Discount,DiscountPrice,Quantity,Amount,Note";

                List<InitConsumerInfo> list = BLLFactory<InitConsumer>.Instance.FindByRoomType(RoomType.Name);
                this.winGridView1.DataSource = new Pager.WinControl.SortableBindingList<InitConsumerInfo>(list);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CListItem item = this.txtItemName.SelectedItem as CListItem;
            if (item != null)
            {
                GoodsInfo info = BLLFactory<Goods>.Instance.FindByID(Convert.ToInt32(item.Value));
                if (info != null)
                {
                    InitConsumerInfo initInfo = new InitConsumerInfo();
                    initInfo.ItemNo = info.ItemNo;
                    initInfo.ItemName = info.Name;
                    initInfo.ItemType = info.GoodsType;
                    initInfo.Price = info.Price;
                    initInfo.Note = this.txtNote.Text;
                    initInfo.Quantity = Convert.ToDouble(this.txtQuantity.Text);
                    initInfo.RoomType = RoomType.Name;
                    initInfo.Amount = Convert.ToDecimal(this.txtAmount.Text);
                    initInfo.Discount = Convert.ToDouble(this.txtDiscountRate.Text);
                    initInfo.DiscountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);

                    try
                    {
                        BLLFactory<InitConsumer>.Instance.Insert(initInfo);
                        BindData();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
        }

        private void txtItemName_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtItemName.SelectedItem as CListItem;
            if (item != null)
            {
                GoodsInfo info = BLLFactory<Goods>.Instance.FindByID(Convert.ToInt32(item.Value));
                if (info != null)
                {
                    this.txtPrice.Text = info.Price.ToString("f2");
                }
            }
        }

        private void txtDiscountPrice_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDiscountPrice.Text) && ValidateUtil.IsNumeric(txtDiscountPrice.Text))
            {
                decimal oldPrice = Convert.ToDecimal(this.txtPrice.Text);
                decimal discountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);
                if (oldPrice > 0 && discountPrice > 0)
                {
                    txtDiscountRate.Text = (discountPrice / oldPrice).ToString("f2");
                }
                else
                {
                    txtDiscountRate.Text = "0";
                }
            }
        }

        private void txtDiscountRate_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDiscountRate.Text) && ValidateUtil.IsNumeric(txtDiscountRate.Text))
            {
                decimal oldPrice = Convert.ToDecimal(this.txtPrice.Text);
                decimal discountRate = Convert.ToDecimal(this.txtDiscountRate.Text);
                if (oldPrice > 0 && discountRate > 0)
                {
                    txtDiscountPrice.Text = (discountRate * oldPrice).ToString("f2");
                }
                else
                {
                    txtDiscountPrice.Text = "0";
                }
            }
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtQuantity.Text) && ValidateUtil.IsNumeric(txtQuantity.Text))
            {
                decimal discountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);
                this.txtAmount.Text = (discountPrice * Convert.ToDecimal(this.txtQuantity.Text)).ToString();
            }
        }

        private void menu_InRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
